﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDuctConnectVectorTerminal : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException("commandData", "is null");
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				ISelectionFilter selectionFilter = new DuctSelectionFilter();
				ISelectionFilter selectionFilter2 = new DuctTerminalVerticalSelectionFilter();
				HYDuctConnect hyductConnect = new HYDuctConnect();
				string text = "风管连接风口";
				string message2 = "";
				RevitVersionFuncs.ClearSelection(selection);
				for (;;)
				{
					transaction = new Transaction(document, text);
					transaction.Start();
					Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择一个风管");
					Duct duct = document.GetElement(reference) as Duct;
					List<Duct> list = new List<Duct>();
					IList<Element> list2 = selection.PickElementsByRectangle(selectionFilter2, "框选要连接的风口，不超过两个");
					bool flag = true;
					RectangleConnTerminalCheck rectangleConnTerminalCheck = new RectangleConnTerminalCheck();
					if (list2 != null && list2.Count > 0)
					{
						if (!rectangleConnTerminalCheck.CheckConnShape(duct, list2))
						{
							message2 = "风口接口类型与风管接口类型不匹配，\n无法进行连接！";
							flag = false;
						}
						else if (list2.Count == 1)
						{
							message2 = "";
							flag = hyductConnect.ConnectVerticalTerminal(list2[0] as FamilyInstance, duct, ref message2, ref list);
						}
						else if (list2.Count == 2)
						{
							message2 = "";
							flag = hyductConnect.ConnectVerticalTerminal(list2[0] as FamilyInstance, list2[1] as FamilyInstance, duct, ref message2, ref list);
						}
					}
					if (!flag)
					{
						YJKMessageBox.Show(message2);
						transaction.RollBack();
					}
					else
					{
						document.Regenerate();
						transaction.Commit();
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				TransactionStatus status = transaction.GetStatus();
                if ((int)status == 1)
				{
					transaction.Commit();
				}
                else if ((int)status == 5)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
